<?php
namespace Core\Mvc\Model;

class Struct
{
    protected $_data;

    public function __construct($data)
    {
        $this->_data = objectToArray($data);
    }

    public function getData()
    {
        return $this->_data;
    }


    public function get($item, $data = null)
    {
        if (is_null($data)) {
            $data = $this->_data;
        }
        $data = objectToArray($data);
        if (isset($data[$item])) {
            return $data[$item];
        }
        foreach ($data as $value) {
            if (isset($value['son']) && is_array($value['son']) && $value['son']) {
                $result = $this->get($item, $value['son']);
                if ($result !== false) {
                    return $result;
                }
            }
        }
        return false;
    }

    public function add($name, $item)
    {
        $this->_data[$name] = $item;
        return $this->_data;
    }

    public function edit($name, $item, $data = null)
    {
        if (is_null($data)) {
            $data = $this->_data;
        }
        if (isset($data[$name])) {
            $data[$name] = $item;
        } else {
            foreach ($data as $key => $value) {
                $data[$key]['son'] = $this->edit($name, $item, $value['son']);
            }
        }

        $this->_data = $data;
        return $this->_data;
    }

    public function delete($item, $data = null)
    {
        if (is_null($data)) {
            $data = $this->_data;
        }

        if (isset($data[$item])) {
            if (isset($data[$item]['son'])) {
                $data = array_merge($data[$item]['son'], $data);
            }
            unset($data[$item]);

            $this->_data = $data;
            return $data;
        }
        foreach ($data as $key => $value) {
            if (isset($value['son'])) {
                $data[$key]['son'] = $this->delete($item, $value['son']);
                if (empty($data[$key]['son'])) {
                    unset($data[$key]['son']);
                }
            }
        }
        $this->_data = $data;
        return $data;
    }

    public static function nestableSort($nestable,$data){
        $data = objectToArray($data);
        $data = self::toData($data);
        return self::_nestableSort($nestable,$data);
    }

    protected static function _nestableSort($nestable,$data){
        $output = [];
        foreach ($nestable as $key => $value){
            if(isset($data[$key])){
                $output[$key] = $data[$key];
                if(is_array($value)){
                    $output[$key]['son'] = self::_nestableSort($value,$data);
                }
            }
        }
        return $output;
    }

    public static function toData($data){
        $output = [];
        foreach ($data as $key => $value){
            if(isset($value['son'])){
                $son = self::toData($value['son']);
                unset($value['son']);
                $output[$key] = $value;
                $output = array_merge($output,$son);
            }else{
                $output[$key] = $value;
            }
        }
        return $output;
    }
}